// TMapNavigate.cpp : implementation file
//

#include "stdafx.h"
//#include "../TBitmap/TNM.h"
#include "TMapNavigate.h"
#include "TMap.h"
//#include "TSetMap.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// TMapNavigate

TMapNavigate::TMapNavigate()
{
}

TMapNavigate::~TMapNavigate()
{
}


BEGIN_MESSAGE_MAP(TMapNavigate, CWnd)
	//{{AFX_MSG_MAP(TMapNavigate)
	ON_WM_CREATE()
	ON_WM_PAINT()
	ON_WM_LBUTTONDOWN()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()


/////////////////////////////////////////////////////////////////////////////
// TMapNavigate message handlers

int TMapNavigate::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CWnd::OnCreate(lpCreateStruct) == -1)
		return -1;
	
	// TODO: Add your specialized creation code here
	UpdateSize();

	return 0;
}

void TMapNavigate::OnPaint() 
{
	CPaintDC dc(this); // device context for painting
	
	// TODO: Add your message handler code here
	CRect rc;
	GetClientRect(&rc);

	dc.StretchBlt(
		0, 0, 
		rc.Width(),
		rc.Height(),
		&(m_map->m_DCsrc),
		0, 0,
		(int)(rc.Width() / m_fZoom),
		(int)(rc.Height() / m_fZoom),
		SRCCOPY);

	CPen pen;
	pen.CreatePen(PS_SOLID, 2, RGB(255,0,0));
	dc.SelectObject(pen);
	dc.MoveTo((int)(m_map->m_rcView.left * m_fZoom), (int)(m_map->m_rcView.top * m_fZoom));
	dc.LineTo((int)(m_map->m_rcView.right * m_fZoom), (int)(m_map->m_rcView.top * m_fZoom));
	dc.LineTo((int)(m_map->m_rcView.right * m_fZoom), (int)(m_map->m_rcView.bottom * m_fZoom));
	dc.LineTo((int)(m_map->m_rcView.left * m_fZoom), (int)(m_map->m_rcView.bottom * m_fZoom));
 	dc.LineTo((int)(m_map->m_rcView.left * m_fZoom), (int)(m_map->m_rcView.top * m_fZoom));
	// Do not call CWnd::OnPaint() for painting messages
}

void TMapNavigate::OnLButtonDown(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	point.x = (int)(point.x / m_fZoom + 0.5f);
	point.y = (int)(point.y / m_fZoom + 0.5f);
	
	m_map->m_ptCenter = point;
	
	m_map->UpdateMap();
	GetParent()->Invalidate();

	CWnd::OnLButtonDown(nFlags, point);
}

void TMapNavigate::UpdateSize()
{
	BITMAP bm;
	GetObject(m_map->m_hBMsrc, sizeof(bm), &bm);
	
	//float zoom_width = (float)((CTNMApp*)AfxGetApp())->GetProfileInt(TSetMap::iKey_naviWidth, 120);
	//zoom_width /= (float)bm.bmWidth;
	//
	//float zoom_height = (float)((CTNMApp*)AfxGetApp())->GetProfileInt(TSetMap::iKey_naviHeight, 120);
	//zoom_height /= (float)bm.bmHeight;

	float zoom_width = (float)(200);
	zoom_width /= (float)bm.bmWidth;
	
	float zoom_height = (float)(200);
	zoom_height /= (float)bm.bmHeight;
	
	m_fZoom = zoom_height > zoom_width ? zoom_width : zoom_height;
	
	MoveWindow(0, 0, (int)(bm.bmWidth * m_fZoom), (int)(bm.bmHeight * m_fZoom));

	Invalidate(FALSE);
}
